export function dbNavPage(url, type = 'navigateTo') {
	function handelNavFail(err) {
		console.warn('tnNavPage', `跳转页面失败: ${err}`);
	}
	return new Promise((resolve, reject) => {
		switch (type) {
			case 'navigateTo':
				uni.navigateTo({
					url,
					success: () => {
						resolve();
					},
					fail: (err) => {
						handelNavFail(err);
						reject(err);
					}
				});
				break;
			case 'redirectTo':
				uni.redirectTo({
					url,
					success: () => {
						resolve();
					},
					fail: (err) => {
						handelNavFail(err);
						reject(err);
					}
				});
				break;
			case 'reLaunch':
				uni.reLaunch({
					url,
					success: () => {
						resolve();
					},
					fail: (err) => {
						handelNavFail(err);
						reject(err);
					}
				});
				break;
			case 'switchTab':
				uni.switchTab({
					url,
					success: () => {
						resolve();
					},
					fail: (err) => {
						handelNavFail(err);
						reject(err);
					}
				});
		}
	});
}

export function dbNavBack() {
	const indexPageUrl = '/pages/index/index';
	// 通过判断当前页面的页面栈信息，是否有上一页进行返回，如果没有则跳转到首页
	const pages = getCurrentPages();
	if (pages?.length) {
		const firstPage = pages[0];
		if (pages.length === 1 && (!firstPage.route || firstPage?.route != indexPageUrl)) {
			return dbNavPage(indexPageUrl, 'reLaunch');
		} else {
			uni.navigateBack({
				delta: 1
			});
		}
	} else {
		return dbNavPage(indexPageUrl, 'reLaunch');
	}
}
export function Toast(title) {
	uni.showToast({
		title: title,
		icon: "none"
	})
}